﻿namespace OleDbDAL.Stock
{
    using DBUtility;
    using IDal.Stock;
    using Model.Stock;
    using System;
    using System.Data;
    using System.Text;

    public class GoodsInOutApp : IGoodsInOut
    {
        private string dbname = "Systems";

        public void Add(GoodsInOutEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_GoodsInOut(");
            builder.Append("ChainNum,DetailsType,BranchId,IsDeptOrBrach,LibraryTypeId,UserId,CreateTime,CheckMan,CheckTime,Remark");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.ChainNum + ",");
            builder.Append("" + model.DetailsType + ",");
            builder.Append("" + model.BranchId + ",");
            builder.Append("" + model.IsDeptOrBrach + ",");
            builder.Append("" + model.LibraryTypeId + ",");
            builder.Append("" + model.UserId + ",");
            builder.Append("'" + model.CreateTime + "',");
            builder.Append("" + model.CheckMan + ",");
            builder.Append("'" + model.CheckTime + "',");
            builder.Append("'" + model.Remark + "'");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int DetailsId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_GoodsInOut ");
            builder.Append(" where DetailsId=" + DetailsId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_GoodsInOut ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by DetailsId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "DetailsId", "T_GoodsInOut", swhere);
        }

        public GoodsInOutEnt GetModel(int DetailsId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select DetailsId,ChainNum,DetailsType,BranchId,IsDeptOrBrach,LibraryTypeId,UserId,CreateTime,CheckMan,CheckTime,Remark from T_GoodsInOut ");
            builder.Append(" where DetailsId=" + DetailsId);
            GoodsInOutEnt ent = new GoodsInOutEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.DetailsId = DetailsId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                if (table.Rows[0]["DetailsType"].ToString() != "")
                {
                    ent.DetailsType = int.Parse(table.Rows[0]["DetailsType"].ToString());
                }
                if (table.Rows[0]["BranchId"].ToString() != "")
                {
                    ent.BranchId = int.Parse(table.Rows[0]["BranchId"].ToString());
                }
                if (table.Rows[0]["IsDeptOrBrach"].ToString() != "")
                {
                    ent.IsDeptOrBrach = int.Parse(table.Rows[0]["IsDeptOrBrach"].ToString());
                }
                if (table.Rows[0]["LibraryTypeId"].ToString() != "")
                {
                    ent.LibraryTypeId = int.Parse(table.Rows[0]["LibraryTypeId"].ToString());
                }
                if (table.Rows[0]["UserId"].ToString() != "")
                {
                    ent.UserId = int.Parse(table.Rows[0]["UserId"].ToString());
                }
                if (table.Rows[0]["CreateTime"].ToString() != "")
                {
                    ent.CreateTime = DateTime.Parse(table.Rows[0]["CreateTime"].ToString());
                }
                if (table.Rows[0]["CheckMan"].ToString() != "")
                {
                    ent.CheckMan = int.Parse(table.Rows[0]["CheckMan"].ToString());
                }
                if (table.Rows[0]["CheckTime"].ToString() != "")
                {
                    ent.CheckTime = DateTime.Parse(table.Rows[0]["CheckTime"].ToString());
                }
                ent.Remark = table.Rows[0]["Remark"].ToString();
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_GoodsInOut ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "DetailsId");
        }

        public void Update(GoodsInOutEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_GoodsInOut set ");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("DetailsType=" + model.DetailsType + ",");
            builder.Append("BranchId=" + model.BranchId + ",");
            builder.Append("IsDeptOrBrach=" + model.IsDeptOrBrach + ",");
            builder.Append("LibraryTypeId=" + model.LibraryTypeId + ",");
            builder.Append("UserId=" + model.UserId + ",");
            builder.Append("CreateTime='" + model.CreateTime + "',");
            builder.Append("CheckMan=" + model.CheckMan + ",");
            builder.Append("CheckTime='" + model.CheckTime + "',");
            builder.Append("Remark='" + model.Remark + "'");
            builder.Append(" where DetailsId=" + model.DetailsId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

